import CryptoJS from './crypto-js/crypto-js.js'

/*
    DES(Data Encryption Standard)和TripleDES是对称加密的两种实现。
    DES和TripleDES基本算法一致，只是TripleDES算法提供的key位数更多，加密可靠性更高。
    DES使用的密钥key为8字节，初始向量IV也是8字节。
    TripleDES使用24字节的key，初始向量IV也是8字节。
*/
//des加密 DES-EDE3-CBC
var keyvi = '12345678';
var key = '12345678'  
var iv = '12345678'   
function des_encrypt(message) {
   /* var key            = CryptoJS.MD5(keyvi).toString();
    var iv            = CryptoJS.MD5(keyvi).toString();
    var crypto_key     = CryptoJS.enc.Utf8.parse(key);
    var crypto_iv     = CryptoJS.enc.Utf8.parse(iv.substr(0,8));
    console.log("加密crypto_iv:"+crypto_iv)
    var encode_str     = CryptoJS.TripleDES.encrypt(message, crypto_key, {    
                     //   iv:         crypto_iv,    
                        mode:         CryptoJS.mode.CBC,    
                        padding:     CryptoJS.pad.Pkcs7});
    return encode_str.toString(); */
    const keyHex = CryptoJS.enc.Utf8.parse(key);  
          const encrypted = CryptoJS.TripleDES.encrypt(message, keyHex, {  
              iv: CryptoJS.enc.Utf8.parse(iv),  
              mode: CryptoJS.mode.CBC,  
              padding: CryptoJS.pad.Pkcs7  
           });  
          return encrypted.toString();  
}
//des解密 DES-EDE3-CBC
function des_decrypt(ciphertext)
{
   /* var key            = CryptoJS.MD5(keyvi).toString();
    var iv            = CryptoJS.MD5(keyvi).toString();
    var crypto_key     = CryptoJS.enc.Utf8.parse(key);
    var crypto_iv     = CryptoJS.enc.Utf8.parse(iv.substr(0,8));
	console.log("解密crypto_iv:"+crypto_iv)
    var decrypt_str    = CryptoJS.TripleDES.decrypt(message, crypto_key, {    
                     //   iv:         crypto_iv,    
                        mode:         CryptoJS.mode.CBC,    
                        padding:     CryptoJS.pad.Pkcs7});
    return     decrypt_str.toString(CryptoJS.enc.Utf8);   */ 
	 const keyHex = CryptoJS.enc.Utf8.parse(key);  
	        // direct decrypt ciphertext  
	        const decrypted = CryptoJS.TripleDES.decrypt({  
	                    ciphertext: CryptoJS.enc.Base64.parse(ciphertext)  
	         }, keyHex, {  
	                    iv: CryptoJS.enc.Utf8.parse(iv),  
	                    mode: CryptoJS.mode.CBC,  
	                    padding: CryptoJS.pad.Pkcs7  
	        });  
	        return decrypted.toString(CryptoJS.enc.Utf8);  
}

module.exports = {
    des_encrypt: des_encrypt,
    des_decrypt: des_decrypt
}